function onOK(){
	// process arguments
	var setz=[];
	var elz=document.getElementsByClassName("setting");
	for(i=0; i<elz.length; i++){
		switch(elz[i].nodeName){
			case "colorpicker":
				setz.push([elz[i].getAttribute("id"),elz[i].getAttribute("color")]);
			break;
			
			case "checkbox":
				setz.push([elz[i].getAttribute("id"),(elz[i].getAttribute("checked")=="true")?true:false]);
			break;
			
			default:
				setz.push([elz[i].getAttribute("id"),elz[i].value]);
			break;
		}
	}
	window.arguments[1].out=setz;
}
function onLoad(){
	try{
		var lastId=0;
		document.title=window.arguments[0];
		gei("TWB-Settings").buttons="accept";
		var setz=window.arguments[1].inn;	
		var SETS=[];
		var i;
		var setting;
		for(i=0; i<setz.length; i++){
			var sub=[
				setz[i][0],
				{},
			];
			for(setting in setz[i][1]){
				if(typeof sub[1][setz[i][1][setting].type]=="undefined"){
					sub[1][setz[i][1][setting].type]=[];
				}
				sub[1][setz[i][1][setting].type].push([setting,lang("setting_"+setting),setz[i][1][setting]]);
			}
			SETS.push(sub);
		}

		// Display
		for(k=0; k<SETS.length; k++){
			// Tab Header
			lastId++;
			var tab=dce("tab");
			tab.setAttribute("label",lang("label_"+SETS[k][0].toLowerCase()));
			tab.setAttribute("name",lastId);
			tab.setAttribute("class","tab-left");
			gei("tabs").appendChild(tab);
			var tabb=dce("tabpanel");
			gei("panels").appendChild(tabb);
			var vbox=dce("vbox");
			vbox.style.overflow="auto";
			vbox.setAttribute("flex","1");
			var grid=dce("grid");
			var cols=dce("columns");
			var col1=dce("column");
			col1.setAttribute("flex","1");
			col2=dce("column");
			col2.setAttribute("flex","1");	
			cols.appendChild(col1);
			cols.appendChild(col2);
			var rows=dce("rows");
			grid.appendChild(cols);
			grid.appendChild(rows);
			vbox.appendChild(grid);
			tabb.appendChild(vbox);
			
			var type;
			for(type in SETS[k][1]){
				for(l=0; l<SETS[k][1][type].length; l++){
					var S=SETS[k][1][type][l];
					var row=dce("row");
					row.setAttribute("name","setting");
					var label=dce("label");
					label.setAttribute("flex","1");
					label.setAttribute("value",S[1]);
					row.appendChild(label);
					if(type=="text"){
						var item=dce("textbox");
						item.setAttribute("id",S[0]);
						item.setAttribute("value",get_var(S[0]));
						item.setAttribute("flex","0");
						item.setAttribute("multiline",false);
						item.setAttribute("class","setting");
						if(typeof S[2].maxlength!="undefined"){
							item.setAttribute("maxlength",S[2].maxlength);
						}
						if(typeof S[2].password!="undefined"){
							item.setAttribute("type","password");
						}
						if(typeof S[2].rows!="undefined"){
							item.setAttribute("rows",S[2].rows);
							item.setAttribute("multiline",true);
						}
						if(typeof S[2].onclick!="undefined"){
							item.addEventListener("click",S[2].onclick,false);
						}
					}
					if(type=="menupop"){
						var item=dce("toolbarbutton");
						item.setAttribute("id",S[0]);
						item.setAttribute("type","menu");
						item.setAttribute("class","setting");
						var holder=dce("menupopup");
						var sel="";
						for(j=0; j<S[2].values.length; j++){
							var option=dce("menuitem");
							option.setAttribute("label",S[2].values[j][1]);
							option.setAttribute("value",S[2].values[j][0]);
							option.addEventListener("click",function(){
								onSelect(this);
							},false);
							holder.appendChild(option);
							if(S[2].values[j][0]==get_var(S[0])){
								sel=S[2].values[j][1];
								holder.selectedIndex=j;
							}
						}
						item.value=get_var(S[0]);
						item.setAttribute("value",get_var(S[0]));
						item.setAttribute("label",sel);
						item.appendChild(holder);
					}
					if(type=="checkbox"){
						var item=dce("hbox");
						var item1=dce("checkbox");
						item1.setAttribute("id",S[0]);
						item1.setAttribute("class","setting");
						item1.setAttribute("checked",get_var(S[0]));
						// Remove fuzz by adding invisble element
						var item2=dce("hbox");
						item.appendChild(item1);
						item.appendChild(item2);
					}
					if(type=="color"){
						var item=dce("hbox");
						item1=dce("colorpicker");
						item1.setAttribute("type","button");
						item1.setAttribute("id",S[0]);
						item1.setAttribute("class","setting");
						item1.setAttribute("color",get_var(S[0]));
						var item2=dce("hbox");
						item.appendChild(item1);
						item.appendChild(item2);
					}
					item.setAttribute("flex","3");
					row.appendChild(item);
					rows.appendChild(row);
				}
			}
		}
		tab=document.getElementsByTagName("tab");
		tab[0].selected=true;
		tab[0]._selected=true;
	}catch(e){alert(e.message);}
}
function onSelect(obj){
	obj.parentNode.parentNode.setAttribute("label",obj.getAttribute("label"));
	obj.parentNode.parentNode.setAttribute("value",obj.getAttribute("value"));
	obj.parentNode.parentNode.value=obj.getAttribute("value");
}
function dce(v){
	return document.createElement(v);
}
function gei(v){
	return document.getElementById(v);
}
function get_var(v,t){
	return window.arguments[2].TWB.get_var(v,t);
}
function lang(v){
	return window.arguments[2].TWB.lang(v);
}
